package com.st.shopadmin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.shopadmin.common.ServerResponse;
import com.st.shopadmin.dao.AdminManagerUserMapper;
import com.st.shopadmin.dao.ManagerUserMapper;
import com.st.shopadmin.pojo.AdminManagerUser;
import com.st.shopadmin.pojo.ManagerUser;
import com.st.shopadmin.pojo.Shop;
import com.st.shopadmin.pojo.ShopUserPlat;
import com.st.shopadmin.service.UserManagerService;
import com.st.shopadmin.util.MD5Util;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserManagerServiceImpl implements UserManagerService {
    @Autowired
    private AdminManagerUserMapper managerUserMapper;
    @Override
    public ServerResponse<PageInfo> getPageLists(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<AdminManagerUser> productList = managerUserMapper.selectList();
        PageInfo pageResult = new PageInfo(productList);
        return  ServerResponse.createBySuccess(pageResult);
    }
    @Override
    public ServerResponse<List> getUserList(int pageNum,int pageSize){
        List<AdminManagerUser> productList = managerUserMapper.selectList();
        return  ServerResponse.createBySuccess(productList);
    }

    @Override
    public ServerResponse<AdminManagerUser> login(String username, String password) {
        int resultCount = managerUserMapper.checkUserName(username);
        if (resultCount == 0){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        String md5Password = MD5Util.MD5EncodeUtf8(password);
        AdminManagerUser user = managerUserMapper.selectLogin(username,md5Password);
        if(user == null){
            return ServerResponse.createByErrorMessage("密码错误");
        }
        user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
        if(user.getAccountStatus()==0){
            return ServerResponse.createByErrorMessage("账号未激活,请联系管理员激活");
        }else if(user.getAccountStatus()==100){
            return  ServerResponse.createByErrorMessage("账号已冻结,请联系管理员激活");
        }
        return ServerResponse.createBySuccess("登陆成功",user);
    }

    @Override
    public ServerResponse<String> register(AdminManagerUser managerUser){
        int resultCount = managerUserMapper.checkUserName(managerUser.getUsername());
        if (resultCount > 0){
            return ServerResponse.createByErrorMessage("用户名已存在");
        }
        resultCount = managerUserMapper.checkEmail(managerUser.getEmail());
        if (resultCount > 0){
            return ServerResponse.createByErrorMessage("邮箱已存在");
        }
        managerUser.setAccountStatus(0);
        managerUser.setUserLevel(1);
        if(null == managerUser.getPassword() || "" == managerUser.getPassword()){
            managerUser.setPassword("111111");
        }
        //MD5加密
        managerUser.setPassword(MD5Util.MD5EncodeUtf8(managerUser.getPassword()));
        resultCount = managerUserMapper.insert(managerUser);
        if(resultCount == 0){
            return  ServerResponse.createByErrorMessage("注册失败");
        }
        return ServerResponse.createBySuccess("注册成功");
    }

    @Transactional
    public  ServerResponse restPassword(Long Id){
        if(Id == null ){
            return   ServerResponse.createByErrorMessage("参数错误");
        }
        AdminManagerUser user = new AdminManagerUser();
        user.setId(Id);
        //MD5加密
        user.setPassword(MD5Util.MD5EncodeUtf8("111111"));
        int rowCount  = managerUserMapper.updateByPrimaryKeySelective(user);
        if(rowCount > 0){
            return  ServerResponse.createBySuccess("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Transactional
    public  ServerResponse activation(Long Id,Integer status){
        if(Id == null ||  status == null){
            return   ServerResponse.createByErrorMessage("参数错误");
        }
        AdminManagerUser user = new AdminManagerUser();
        user.setId(Id);
        user.setAccountStatus(status);
        int rowCount  = managerUserMapper.updateByPrimaryKeySelective(user);
        if(rowCount > 0){
            return  ServerResponse.createBySuccess("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Transactional
    public  ServerResponse frozen(Long Id,Integer status){
        if(Id == null ||  status == null){
            return   ServerResponse.createByErrorMessage("参数错误");
        }
        AdminManagerUser user = new AdminManagerUser();
        user.setId(Id);
        user.setAccountStatus(status);
        int rowCount  = managerUserMapper.updateByPrimaryKeySelective(user);
        if(rowCount > 0){
            return  ServerResponse.createBySuccess("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

}
